package com.slink.fxfk.framework.inteceptor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.slink.fxfk.common.domain.bo.CookieUser;
import com.slink.fxfk.framework.util.CookieUtil;
import com.slink.fxfk.framework.util.SessionUtil;

public class CookieInteceptor implements HandlerInterceptor {
	// 无需安全认证的url
	private List<String> excludeUrl = new ArrayList<String>();
	@Autowired
	private Object memberService;

	@SuppressWarnings("unused")
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String url = request.getRequestURI().replaceFirst(
				request.getContextPath(), "");

		for (int i = 0, len = this.excludeUrl.size(); i < len; i++) {
			if (url.matches(this.excludeUrl.get(i))) {
				return true;
			}
		}
		Object memberInfo = SessionUtil.getMemberSession(request);
		// 判断用户是否登陆
		if (memberInfo == null) {
			Cookie[] cookies = request.getCookies();
			if (cookies != null && cookies.length > 0) {
				String cookieValue = null;
				for (int i = 0; i < cookies.length; i++) {
					if (CookieUtil.cookieDomainName
							.equals(cookies[i].getName())) {
						cookieValue = cookies[i].getValue();
						break;
					}
				}
				if (null != cookieValue) {
					// 解码
					String cookieValueAfterDecode = new String(
							Base64.decodeBase64(cookieValue.getBytes()),
							"utf-8");
					String[] cookieValues = cookieValueAfterDecode.split("_");
					if (cookieValues.length == 3) {
						// 有效期判断
						long validTimeInCookie = new Long(cookieValues[1]);
						if (validTimeInCookie < System.currentTimeMillis()) {
							// 删除cookie
							CookieUtil.clearCookie(response);
						} else {
							// 取出会员email
							String email = cookieValues[0];
							CookieUser bMember = new CookieUser();

							if (bMember != null) {

								String md5ValueInCookie = cookieValues[2];
								String md5ValueFromMember = CookieUtil
										.getMD5(bMember.getUserEmail() + "_"
												+ bMember.getPassWord() + "_"
												+ validTimeInCookie + "_"
												+ CookieUtil.webKey);
								if (md5ValueFromMember.equals(md5ValueInCookie)) {

								} else {
									// 删除cookie
									CookieUtil.clearCookie(response);
								}
							} else {
								// 删除cookie
								CookieUtil.clearCookie(response);
							}
						}
					}
				}
			}
		}

		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

	public List<String> getExcludeUrl() {
		return excludeUrl;
	}

	public void setExcludeUrl(List<String> excludeUrl) {
		this.excludeUrl = excludeUrl;
	}

}
